//package com.ruoyi.web.controller.example;
//
//import com.ruoyi.common.annotation.Anonymous;
//import com.ruoyi.common.core.domain.AjaxResult;
//import com.ruoyi.common.exception.BizException;
//import io.lettuce.core.dynamic.annotation.Value;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiOperation;
//import org.springframework.core.io.FileSystemResource;
//import org.springframework.core.io.InputStreamResource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.IOException;
//@Anonymous
//@RestController
//@RequestMapping("/file")
//@Api(tags="文件操作")
//public class FileController {
//    private static final File UPLOAD_FOLDER = ;
//    @Value("${ruoyi.profile}")
//    private String uploadFolder;
//    public AjaxResult upload(MultipartFile file) {
//        return AjaxResult.success("文件上传成功");
//    }
//    File destFile = new File(uploadFolder + "/" + fileName);
//    private MultipartFile file;
//    String fileName = file.getOriginalFilename();
//     File folder = new File(uploadFolder);
//        if(!folder.exists()) {
//        folder.mkdirs();
//    }
//    @ApiOperation("文件上传")
//    @PostMapping
//    public AjaxResult upload(@RequestPart("file") MultipartFile file) {
//        // 1 首先判断file是不是为空，空的场合抛出异常
//        if(file.isEmpty()) {
//            throw new BizException(404, "文件不能为空");
//        }
//        // 2 在判断服务器上的目录存不存在，不存在创建目录
//        File folder = new File(uploadFolder);
//        if(!folder.exists()) {
//            folder.mkdirs();
//        }
//        // 3 在服务器目录里创建一个同名的空文件
//        String fileName = file.getOriginalFilename();
//        File destFile = new File(uploadFolder + "/" + fileName);
//        // 4 把传过来的文件内容写入到空的文件中
//        try {
//            file.transferTo(destFile);
//        } catch (IOException e) {
//            throw new BizException(500, "文件上传失败");
//        }
//
//        return AjaxResult.success("文件上传成功");
//    }
//    @ApiOperation(value = "文件下载", notes = "下载服务器上文件到本地")
//    @ApiImplicitParam(name = "name", value = "文件名", required = true, dataType = "string", paramType = "query",
//            dataTypeClass = String.class)
//    @GetMapping(value = "/download")
//    public ResponseEntity<InputStreamResource> download(@RequestParam("name") String name) {
//        FileSystemResource file = new FileSystemResource(UPLOAD_FOLDER + "/" + name);
//        HttpHeaders headers = new HttpHeaders();
//        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
//        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"",
//                file.getFilename()));
//        headers.add("Pragma", "no-cache");
//        headers.add("Expires", "0");
//        try {
//            return ResponseEntity
//                    .ok()
//                    .headers(headers)
//                    .contentLength(file.contentLength())
//                    .contentType(MediaType.parseMediaType("application/octet-stream"))
//                    .body(new InputStreamResource(file.getInputStream()));
//        } catch (IOException e) {
//            throw new BizException(500, "文件下载失败");
//        }
//    }
//    @ApiOperation(value = "文件下载", notes = "下载服务器上文件到本地")
//    @ApiImplicitParam(name = "name", value = "文件名", required = true, dataType = "string", paramType = "query",
//            dataTypeClass = String.class)
//    @GetMapping(value = "/download")
//    public ResponseEntity<InputStreamResource> download(@RequestParam("name") String name) {
//        FileSystemResource file = new FileSystemResource(uploadFolder + "/" + name);
//        HttpHeaders headers = new HttpHeaders();
//        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
//        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"",
//                file.getFilename()));
//        headers.add("Pragma", "no-cache");
//        headers.add("Expires", "0");
//        try {
//            return ResponseEntity
//                    .ok()
//                    .headers(headers)
//                    .contentLength(file.contentLength())
//                    .contentType(MediaType.parseMediaType("application/octet-stream"))
//                    .body(new InputStreamResource(file.getInputStream()));
//        } catch (IOException e) {
//            throw new BizException(500, "文件下载失败");
//        }
//    }
//}
